package com.xsy.carRental.controller;

import com.xsy.carRental.constant.MessageConstant;
import com.xsy.carRental.dto.MaintenanceAddDTO;
import com.xsy.carRental.dto.MaintenanceQueryDTO;
import com.xsy.carRental.result.PageResult;
import com.xsy.carRental.result.Result;
import com.xsy.carRental.service.MaintenanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/maintenance")
@Slf4j
public class MaintenanceController {
    @Autowired
    private MaintenanceService maintenanceService;

    /**
     * 根据车辆id新增维修单
     *
     * @param maintenanceAddDTO
     * @return
     */
    @PostMapping()
    public Result maintenanceAdd(@RequestBody MaintenanceAddDTO maintenanceAddDTO) {
        log.info("维修单为{}", maintenanceAddDTO);
        maintenanceService.add(maintenanceAddDTO);

        return Result.success(MessageConstant.MAINTENANCEADD_SUCCESS);
    }

    /**
     * 可选择根据车辆id查询维修单
     *
     * @param maintenanceQueryDTO
     * @return
     */
    @GetMapping()
    public Result<PageResult> pageQuery(MaintenanceQueryDTO maintenanceQueryDTO) {
        log.info("分页查询维修单{}", maintenanceQueryDTO);
        PageResult pageResult = maintenanceService.SearchQuery(maintenanceQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据维修id删除维修单
     *
     * @param maintenanceId
     * @return
     */
    @DeleteMapping("/{maintenanceId}")
    public Result deleteRental(@PathVariable Integer maintenanceId, @RequestParam int vehicleId) {//这里接收的id是车辆的ID
        log.info("删除车辆id为{} 的维修单", vehicleId);
        maintenanceService.delete(maintenanceId, vehicleId);
        return Result.success();
    }
}
